﻿using System;
using Topshelf;

namespace Sustainalytics.ClientInterface.AngularHosting.Service
{
    class Program
    {
        private const string ServiceName = "Sustainalytics.ClientInterface.AngularHosting.Service";

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;            

            HostFactory.Run(factory =>
            {
                // Provide the service's behavior using our custom
                //  ServiceHost class
                //
                factory.Service<ServiceHost>(service =>
                {
                    service.ConstructUsing(name => new ServiceHost());
                    service.WhenStarted(sh => sh.Start());
                    service.WhenShutdown(sh => sh.Shutdown());
                    service.WhenStopped(sh => sh.Stop());
                });

                // Now define some attributes of the service overall
                //
                factory.RunAsLocalSystem();

                // Provide the metadata to the service control
                //     x.SetServiceName(ServiceName);

                factory.SetDisplayName(ServiceName);
                factory.SetDescription("Hosts the " + ServiceName.Replace(".", " "));

            });
        }

        private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject.ToString());
        }
    }
}
